//
//  FRAdManager.m
//  Fresh Reading
//
//  Created by Annie on 2020/5/25.
//  Copyright © 2020 Annie. All rights reserved.
//

#import "FRAdManager.h"

@implementation FRAdManager


+ (instancetype)manager{
    static dispatch_once_t onceToken;
    static FRAdManager *instance;
    dispatch_once(&onceToken, ^{
        FRAdManager *manager = [FRAdManager new];
        
        
        BOOL test = YES;
        test = NO;
//        if (test) {
//            // 插屏
//            FRAdConfig *config = [FRAdConfig new];
//            config.fr_showAd = YES;
//            config.fr_type = kFRADTypeInterstitial;
//            config.fr_reading_pages = 12;
//            config.fr_slotID_csj = @"945195225";
//            manager.pg_reading_config = config;
//        }
        {
            // 信息流
            FRAdConfig *config = [FRAdConfig new];
            config.fr_showAd = test;
            config.fr_reading_pages = 12;
            config.fr_type = kFRADTypeNative;
            config.fr_slotID_csj = @"945195313";
            manager.pg_reading_native_config = config;
        }
        {
            // Banner
            FRAdConfig *config = [FRAdConfig new];
            config.fr_showAd = test;
            config.fr_type = kFRADTypeBanner;
            config.fr_slotID_csj = @"945197410";
            manager.pg_reading_banner_config = config;
        }
         {
            // 开屏
            FRAdConfig *config = [FRAdConfig new];
            config.fr_showAd = test;
            config.fr_type = kFRADTypeSplash;
            config.fr_slotID_csj = @"887327483";
            manager.pg_lunch_config = config;
        }
        
        [manager uploadAdConfig];
        
        instance = manager;
        
    });
    return instance;
}

- (FRAdConfig *)adConfigWithType:(kFRADType)adType show:(BOOL)show slotID:(NSString *)slotID{
    FRAdConfig *config = [FRAdConfig new];
    config.fr_showAd = show;
    config.fr_type = adType;
    if (config.fr_type == kFRADTypeSplash) {
        config.fr_slotID_csj = @"887327483";
    }else if (config.fr_type == kFRADTypeBanner) {
        config.fr_slotID_csj = @"945197410";
    }else if (config.fr_type == kFRADTypeNative) {
        config.fr_slotID_csj = @"945195313";
    }else if (config.fr_type == kFRADTypeInterstitial) {
        config.fr_slotID_csj = @"945195225";
    }
    return config;
}

- (void)uploadAdConfig{
    
    NSMutableDictionary *parameter = [NSMutableDictionary dictionary];
    NSDictionary *bundleDic = [[NSBundle mainBundle] infoDictionary];
    NSString *version =  [bundleDic objectForKey:@"CFBundleShortVersionString"];
    NSString *bd =  [bundleDic objectForKey:@"CFBundleName"];
    [parameter setValue:version forKey:@"version"];
    [parameter setValue:bd forKey:@"package"];
    
    [FRNetwork fr_requestWithPath:kFRConfigGetAdsOperation type:kFRNetworkRequestPost parameter:parameter resultBlock:^(FRNetworkModel * _Nonnull model) {
        if (model.code == 1) {
            self.fr_loadSuccess = YES;
            NSDictionary *dict = (NSDictionary *)model.data;
            NSLog(@"%@",dict);
            NSArray<NSString *> *allKeys = [dict allKeys];
            for (NSString *key in allKeys) {
                
                if ([key isEqualToString:@"1"]) {
                    NSNumber *value = [dict valueForKey:key];
                    self.pg_lunch_config.fr_showAd = [value boolValue];
                }else if ([key isEqualToString:@"1-setting"]){
                    
                }else if ([key isEqualToString:@"1-setting-slot-csj"]){
                    NSString *value = [dict valueForKey:key];
                    self.pg_lunch_config.fr_slotID_csj = value;
                }
                
                else if ([key isEqualToString:@"2"]){
                    NSNumber *value = [dict valueForKey:key];
                    self.pg_reading_native_config.fr_showAd = [value boolValue];
                }else if ([key isEqualToString:@"2-setting"]){
                    NSNumber *value = [dict valueForKey:key];
                    self.pg_reading_native_config.fr_reading_pages = value.integerValue;
                }else if ([key isEqualToString:@"2-setting-slot-csj"]){
                    NSString *value = [dict valueForKey:key];
                    self.pg_reading_native_config.fr_slotID_csj = value;
                }
                
                else if ([key isEqualToString:@"100"]){
                    NSNumber *value = [dict valueForKey:key];
                    self.pg_reading_banner_config.fr_showAd = [value boolValue];
                }else if ([key isEqualToString:@"100-setting"]){

                }

                
//                else if ([key isEqualToString:@"11"]){
//                    NSNumber *value = [dict valueForKey:key];
//                    self.pg_reading_banner_config.fr_showAd = [value boolValue];
//                }else if ([key isEqualToString:@"11-setting"]){
//
//                }
//
//                else if ([key isEqualToString:@""]){
//
//                }else if ([key isEqualToString:@""]){
//
//                }

            }
            
            
            
        }else{
            
        }
    }];
}




@end
